int main(int argc, char *argv[])
{
  int fd, s;
  int len;
  volatile int true = 1;
  struct sockaddr_in ec;
  struct sockaddr_in server_sockaddr;

  pthread_t th_key;
  void * retval;

  /*第一步，进行（中断）信号的处理*/
  signal(SIGCHLD, SIG_IGN);//见1
  signal(SIGPIPE, SIG_IGN);
  signal(SIGALRM, sigalrm);

  /*切换工作目录*/
  chroot(HTTPD_DOCUMENT_ROOT);//见2
  printf("starting httpd...\n");
  printf("press q to quit.\n");
//  chdir("/");

  /*第二步，输入命令并进行处理*/
  if (argc > 1 && !strcmp(argv[1], "-i")) {
/* I'm running from inetd, handle the request on stdin */
fclose(stderr);
HandleConnect(0);//调用之前定义的函数，见3
exit(0);
  }

  /*第三步，建立socket接口*/
  if((s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == -1) {
perror("Unable to obtain network");
exit(1);
  }

  if((setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *)&true, 
         sizeof(true))) == -1) {
perror("setsockopt failed");
exit(1);
  }

  /*定义server_sockaddr结构体中的内容*/
  server_sockaddr.sin_family = AF_INET;
  server_sockaddr.sin_port = htons(SERVER_PORT);
  server_sockaddr.sin_addr.s_addr = htonl(INADDR_ANY);

  /*第四步，对端口和地址进行绑定*/
  if(bind(s, (struct sockaddr *)&server_sockaddr, 
      sizeof(server_sockaddr)) == -1)  {
perror("Unable to bind socket");
exit(1);
  }

  /*第五步，监听端口*/
  if(listen(s, 8*3) == -1) { /* Arbitrary, 8 files/page, 3 clients */
perror("Unable to listen");
exit(4);
  }

    /*第六步，多线程创建，等待连接*/
    pthread_create(&th_key, NULL, key, 0);
  /* Wait until producer and consumer finish. */
  printf("wait for connection.\n"); 
  while (1) {

len = sizeof(ec);

/*第七步，接收客户端连接请求*/
if((fd = accept(s, (void *)&ec, &len)) == -1) {
  exit(5);
  close(s);
}
HandleConnect(fd);

  }

  /*第八步，等待线程连接结束之后再结束main函数*/
  pthread_join(th_key, &retval);
}

